package com.example.flink.wordcount;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * Created with IntelliJ IDEA.
 * ClassName: StreamDemo1
 * Package: com.example.flink.wordcount
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-07-20
 * Time: 21:08
 */

//转换算子 flatMap
public class StreamDemo1 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        final DataStreamSource<String> data = env.readTextFile("./word.txt");

        //输出带有a的单词
        final SingleOutputStreamOperator<String> flatMap = data.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public void flatMap(String value, Collector<String> out) throws Exception {
                final String[] s = value.split(" ");
                for (String s1 : s) {
                    if (s1.contains("a")) {
                        out.collect(s1);
                    }
                }
            }
        });

        flatMap.print();
        env.execute();
    }
}
